package com.mall.order.services;

import com.mall.order.OrderDeleteService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.OrderDeleteRequest;
import com.mall.order.dto.OrderDeleteResponse;
import com.mall.order.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Service
public class OrderDeleteServiceImpl implements OrderDeleteService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public OrderDeleteResponse deleteOrder(OrderDeleteRequest request) {
        OrderDeleteResponse response = new OrderDeleteResponse();
        try {
            orderMapper.deleteByPrimaryKey(request.getOrderId());
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(request.getOrderId());
            OrderShipping orderShipping = new OrderShipping();
            orderShipping.setOrderId(request.getOrderId());
            orderItemMapper.delete(orderItem);
            orderShippingMapper.delete(orderShipping);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.error("OrderDeleteServiceImpl.deleteOrder occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
